﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.BusinessLogic;
using DiscoveryLogic.Analytics.BusinessLogic.Networks;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.UI.Common.Events;
using DiscoveryLogic.Common.Utilities;
using Microsoft.Practices.Composite.Events;

namespace DiscoveryLogic.Analytics.Modules.DataNavigation
{
    public class NetworkTreePresenter
    {
        private NetworkTreeView _View;
		/// <summary></summary>
		public NetworkTreeView View
		{
			get
			{
				return this._View;
			}
			set
			{
				this._View = value;
				if(this._View !=null)
				{
					this._View.NetworkSelected += new NetworkTreeView.OnNetworkSelected(SelectNetwork);
                    this._View.ViewNetworkData += new NetworkTreeView.OnViewNetworkData(_View_ViewNetworkData);
                    this._View.ViewNetworkGraph += new NetworkTreeView.OnViewNetworkGraph(_View_ViewNetworkGraph);
                    this._View.ViewRadialGraph += new NetworkTreeView.OnViewRadialGraph(_View_ViewRadialGraph);
                    this._View.ViewNetworkStats += new NetworkTreeView.OnViewNetworkStats(_View_ViewNetworkStats);
				    this._View.PopulateTreeView(this.ReadNetworks());
				}
			}
		}

        void _View_ViewRadialGraph(Network network)
        {
            if (network != null)
            {
                this._EventAggregator.GetEvent<ViewRadialGraphEvent>().Publish(network);
            }
        }

        void _View_ViewNetworkStats(Network network)
        {
            if (network != null)
            {
                this._EventAggregator.GetEvent<ViewNetworkStatsEvent>().Publish(network);
            }
        }

        void _View_ViewNetworkGraph(Network network)
        {
            if (network != null)
            {
                this._EventAggregator.GetEvent<ViewNetworkGraphEvent>().Publish(network);
            }
        }

        void _View_ViewNetworkData(Network network)
        {
            if (network != null)
            {
                this._EventAggregator.GetEvent<ViewNetworkDataEvent>().Publish(network);
            }
        }

		private readonly IEventAggregator _EventAggregator;

        public NetworkTreePresenter(IEventAggregator eventAggregator)
		{
			this._EventAggregator = eventAggregator;
		}

		public void SelectNetwork(Network network)
		{
            if (network != null)
			{
				this._EventAggregator.GetEvent<NetworkSelectedEvent>().Publish(network);
			}
		}

        private ObservableCollection<NetworksBySource> ReadNetworks()
        {
            //ObservableCollection<NetworksBySource> allNetworks=new ObservableCollection<NetworksBySource>();

            //List<NetworkDefinition> dbNetworks = NetworkManager.GetNetworkDefs(SetDataSourceType.DB, "../images/database.ico");
            //allNetworks.Add(new NetworksBySource(SetDataSourceType.DB.ToString(), dbNetworks));

            //List<NetworkDefinition> xlNetworks = NetworkManager.GetNetworkDefs(SetDataSourceType.ExcelFile, "../images/excel.ico");
            //allNetworks.Add(new NetworksBySource(SetDataSourceType.ExcelFile.ToString(), xlNetworks));

            //return allNetworks;
            MyData.ReadAllNetworks();
            return MyData.AllNetworks;
        }
    }
}
